package com.example.myspringboot.web;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cy
 * @date 2024/1/5
 */
@RestController
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Log log = LogFactory.get();

    @ExceptionHandler(Exception.class)
    public String customException(Exception e) {
        log.error(e.getMessage(), e);
        JSONObject res = JSONUtil.createObj();
        res.set("code", "900");
        res.set("msg", e.getMessage());
        return res.toString();
    }
}
